import { http } from '@/utils/http'
// 引入ts类型
import type {
  orderDetailType,
  preOrderType,
  submitOrderParamsType,
  payType,
  orderLogisticsType,
  nowBuyParamsType,
  orderListType,
} from '@/types/order'

enum API {
  GETPREORDERAPI = '/member/order/pre',
  SUBMITRDERAPI = '/member/order',
  AGAINBUYAPI = '/member/order/repurchase/',
  WXPAYAPI = '/pay/wxPay/miniPay',
  MOCKPAYAPI = '/pay/mock',
  DELIVERGOODSAPI = '/member/order/consignment/',
  ORDERLOGISTICSAPI = '/member/order/',
  NOWBUYAPI = '/member/order/pre/now',
}

// 获取预付订单
export const getPreOrderAPI = () => {
  return http<preOrderType>({
    url: API.GETPREORDERAPI,
    method: 'GET',
  })
}

// 提交订单的接口
export const postSubmitOrderAPI = (data: submitOrderParamsType) => {
  return http<{ id: string }>({
    url: API.SUBMITRDERAPI,
    method: 'POST',
    data,
  })
}

// 获取订单详情的接口
export const getOrderDetailAPI = (id: string) => {
  return http<orderDetailType>({
    url: `${API.SUBMITRDERAPI}/${id}`,
    method: 'GET',
  })
}

// 取消订单的接口
export const cancelOrderAPI = (id: string, cancelReason: string = '') => {
  return http<orderDetailType>({
    url: `${API.SUBMITRDERAPI}/${id}/cancel`,
    method: 'PUT',
    data: {
      cancelReason,
    },
  })
}

// 再次购买的接口
export const againBuyAPI = (id: string) => {
  return http<preOrderType>({
    url: API.AGAINBUYAPI + id,
    method: 'GET',
  })
}

// 微信支付的接口
export const getWxPayAPI = (orderId: string) => {
  return http<payType>({
    url: API.WXPAYAPI + '?orderId=' + orderId,
    method: 'GET',
  })
}

// 模拟支付的接口
export const getMockPayAPI = (orderId: string) => {
  return http<{ msg: string }>({
    url: API.MOCKPAYAPI + '?orderId=' + orderId,
    method: 'GET',
  })
}

// 模拟发货的接口
export const getDeliverGoodsAPI = (id: string) => {
  return http<{ msg: string }>({
    url: API.DELIVERGOODSAPI + id,
    method: 'GET',
  })
}

// 获取订单物流的接口
export const getOrderLogisticsAPI = (id: string) => {
  return http<orderLogisticsType>({
    url: `${API.ORDERLOGISTICSAPI}${id}/logistics`,
    method: 'GET',
  })
}

// 确认收货的接口
export const putConfirmReceiptAPI = (id: string) => {
  return http<orderDetailType>({
    url: `${API.ORDERLOGISTICSAPI}${id}/receipt`,
    method: 'PUT',
  })
}

// 删除订单的接口
export const deleteOrderAPI = (ids: string[]) => {
  return http<{ msg: string }>({
    url: API.SUBMITRDERAPI,
    method: 'DELETE',
    data: {
      ids,
    },
  })
}

// 立即购买的接口
export const nowBuyAPI = (data: nowBuyParamsType) => {
  return http({
    url: API.NOWBUYAPI,
    method: 'GET',
    data,
  })
}

// 获取订单列表
export const getOrderListAPI = (page: number = 1, pageSize: number = 10, orderState: number = 0) => {
  return http<orderListType>({
    url: API.SUBMITRDERAPI,
    method: 'GET',
    data: {
      page,
      pageSize,
      orderState,
    },
  })
}
